package cqut.gbq.qimo.controller;

import cqut.gbq.qimo.entity.Assignment;
import cqut.gbq.qimo.entity.Course;
import cqut.gbq.qimo.entity.ResBean;
import cqut.gbq.qimo.entity.Student;
import cqut.gbq.qimo.service.CourseService;
import cqut.gbq.qimo.service.FileService;
import cqut.gbq.qimo.service.StudentsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

/**
 * @ClassName StudentController
 * @Description TODO
 * @Author guobenqi
 * @Date 2021/6/16 13:38
 */
@Api(tags = "学生相关接口")
@RestController
public class StudentController {

    @Autowired
    private StudentsService studentsService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private FileService fileService;

    @ApiOperation("获取全部学生")
    @GetMapping("/allstudents")
    private ResBean getAllStudents(){

        List<Student> list = studentsService.findAllStudent();
        return  ResBean.success("获取成功",list);
    }

    @ApiOperation("获得学生的班级信息")
    @GetMapping("/classInfo")
    private ResBean getClassInfo(@RequestParam Integer sid){
        System.out.println(sid);

        List<Course> courseList =  studentsService.getClassInfo(sid);

        return  ResBean.success("获取成功",courseList);
    }

    @ApiOperation("获得班级的作业信息")
    @GetMapping("/assignmentList")
    private ResBean getAssignmentList(@RequestParam Integer sid){

        List<Course> courseList = courseService.getClassInfo(sid);

        return  ResBean.success("获取成功",courseList);
    }

    @ApiOperation("加入课程")
    @PostMapping("/joinCourse")
    public ResBean joinCourse(@RequestParam Integer sid,@RequestParam String code){

        return studentsService.joinCourse(sid,code);

    }

    @ApiOperation("学生退课")
    @PostMapping("/dropclass")
    public ResBean joinCourse(@RequestParam Integer sid,@RequestParam Integer cid){

        System.out.println(sid);
        System.out.println(cid);
        return studentsService.dropCourse(sid,cid);

    }

    @ApiOperation("获得课程信息")
    @GetMapping("/getCourse")
    public ResBean joinCourse(@RequestParam Integer cid){


        System.out.println(cid);

//        return studentsService.dropCourse(sid,cid);
        return courseService.getCourseInfo(cid);
//        return ResBean.success("获取人员数量成功");

    }


    @ApiOperation("获得课程人员数量")
    @GetMapping("/getcourseCount")
    public ResBean getcourseCount(@RequestParam Integer cid){


        System.out.println(cid);
        return courseService.getCourseCount(cid);

    }


    @ApiOperation("获得课程作业")
    @GetMapping("/getAssignment")
    public ResBean getAssignment(@RequestParam Integer cid,@RequestParam Integer sid){
        System.out.println(cid);
        System.out.println(sid);


        System.out.println(cid);
        return courseService.getAssignment(cid,sid);

    }


    @ApiOperation("获得作业详情")
    @GetMapping("/getStuWork")
    public ResBean getStuWork(@RequestParam Integer aid ,@RequestParam Integer sid){
        System.out.println(aid);
        System.out.println(sid);

        return courseService.getStuWork(aid,sid);

    }

    @ApiOperation("上传文件")
    @PostMapping("/file/upload")
    public ResBean uploadFile(@RequestParam("file") MultipartFile file,@RequestParam Integer sid,@RequestParam Integer aid){


        return  fileService.uploadFile(file,sid,aid);

    }
    @ApiOperation("获取学生基本信息")
    @GetMapping("/getStuInfo")
    public ResBean getStuInfo(@RequestParam Integer sid){


        return  studentsService.getStuInfo(sid);

    }

    @ApiOperation("修改学生基本信息")
    @PostMapping("/editStudent")
    public ResBean editStudent(@RequestBody Student student){

        System.out.println(student);
        return  studentsService.editStuInfo(student);
//        return  ResBean.success("成功");

    }

    @ApiOperation("增加学生置顶课程")
    @GetMapping("/updateStudentTop")
    public ResBean updateTop(@RequestParam Integer cid, Integer sid){
        Integer row = studentsService.updateTop(cid, sid);
        if (row == 1){
            return ResBean.success("置顶成功");
        } else {
            return ResBean.error("置顶失败");
        }
    }

    @ApiOperation("取消学生置顶课程")
    @GetMapping("/updateStudentNoTop")
    public ResBean updateNoTop(@RequestParam Integer cid, Integer sid){
        Integer row = studentsService.updateNoTop(cid, sid);
        if (row == 1){
            return ResBean.success("取消置顶成功");
        } else {
            return ResBean.error("取消置顶失败");
        }
    }

    @ApiOperation("查询学生置顶课程")
    @GetMapping("/getStudentCourseTop")
    public ResBean getCourseTop(@RequestParam Integer sid){
        List<Course> courseTop = studentsService.getCourseTop(sid);
        if (courseTop != null){
            courseTop.forEach(item->{
                List<Assignment> assignments = courseService.getAssignmentLinkCourse(item.getCid());
                item.setAssignmentList(assignments);
                System.out.println("");
            });
            return ResBean.success("查询置顶成功",courseTop);
        } else {
            return ResBean.error("查询置顶失败");
        }
    }

    @ApiOperation("查询学生其他课程")
    @GetMapping("/getStudentOtherCourseTop")
    public ResBean getOtherCourseTop(@RequestParam Integer sid){
        List<Course> otherCourseTop = studentsService.getOtherCourseTop(sid);
        if (otherCourseTop != null){
            otherCourseTop.forEach(item->{
                List<Assignment> assignments = courseService.getAssignmentLinkCourse(item.getCid());
                item.setAssignmentList(assignments);
                System.out.println("");
            });
            return ResBean.success("查询置顶成功",otherCourseTop);
        } else {
            return ResBean.error("查询置顶失败");
        }
    }


}
